<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    class MySet {
      constructor () {
        this.items = {}
      }
      // 1. 检查是否有该元素
      has (value) {
        if (!value) {
          return console.error('请输入正确的值')
        }
        return this.items.hasOwnProperty(value)
      }
      // 2. 增加元素
      add (value) {
        if (this.has(value)) return false
        this.items[value] = value
        return this.items
      }
      // 3. 删除元素
      remove (value) {
        if (!value) return console.error('请输入合法的值')
        if (!this.has(value)) return console.log('找不到该值')
        delete this.items[value]
        return true
      }
      // 4. 展示集合
      values () {
        const result = []
        Object.keys(this.items).forEach(value => {
          result.push(value)
        })
        return result
      }
      // 5. 移除集合的所有项
      clear () {
        this.items = {}
        return '清空成功'
      }
      // 6. 返回有多少数据
      size () {
        return Object.keys(this.items).length
      }
      // 7. 并集
      union (otherSet) {
        if (otherSet.constructor.name !== 'MySet') return console.error('请传入正确的集合')
        const newSet = new MySet()
        Object.keys(this.items).forEach(value => {
          newSet.add(value)
        })
        Object.keys(otherSet.items).forEach(value => {
          newSet.add(value)
        })
        return newSet
      }
      // 交集
      intersection (otherSet) {
        if (otherSet.constructor.name !== 'MySet') return console.error('请传入正确的集合')
        const newSet = new MySet()
        Object.keys(otherSet.items).forEach(value => {
          if (this.has(value)) newSet.add(value)
        })
        return newSet
      }
      // 差集
      diffrence (otherSet) {
        if (otherSet.constructor.name !== 'MySet') return console.error('请传入正确的集合')
        const newSet = new MySet()
        Object.keys(this.items).forEach(value => {
          if (!otherSet.has(value)) newSet.add(value) 
        })
        return newSet
      }
      subset (otherSet) {
        if (otherSet.constructor.name !== 'MySet') return console.error('请传入正确的集合')
        let result = true
        Object.keys(this.items).forEach(value => {
          if (!otherSet.has(value)) result = false
        })
        return result
      }
    }
    const test = new MySet()
    console.log(test.add('aaa'))
    console.log(test.add('bbb'))
    // console.log(test.remove('aaa'))
    // console.log(test.has('aaa'))
    // console.log(test.values())
    // console.log(test.size())
    // console.log(test.__proto__.constructor)
    const test1 = new MySet()
    test1.add('ccc')
    test1.add('bbb')
    // console.log(test.union(test1).values())
    // console.log(test.intersection(test1).values())
    // console.log(test.diffrence(test1).values())
    console.log(test.subset(test1))
    test1.add('aaa')
    console.log(test.subset(test1))
  </script>
</body>
</html>